package gurulab.merlin.server;

import gurulab.merlin.core.CantHandException;
import gurulab.merlin.core.envelope.Envelope;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * @author Cesar Obach-Renner, cesar AT gurulab.org
 * @version 1.0
 */
public class Nodes {
    public Map nodes;
    
    /**
     * Instancia 
     * @author Cesar Obach-Renner, cesar AT gurulab.org
     */    
    public Nodes (){
        nodes=new HashMap ();
    }
    
    /**
     *
     * @param alias
     * @param container
     * @return int
     * @author Cesar Obach-Renner, cesar AT gurulab.org
     */
    public int put (String alias,EPContainer container){
        if(nodes.get (alias)==null){
            nodes.put (alias,container);
            return 0;
        } else{
            return -1;
        }
        
    }
    
    /**
     *
     * @param alias
     * @return EPContainer
     * @author Cesar Obach-Renner, cesar AT gurulab.org
     */
    public EPContainer get (String alias){
        return((EPContainer)nodes.get (alias));
    }
    
    /**
     * @return int
     * @author Cesar Obach-Renner, cesar AT gurulab.org
     */
    public int size (){
        return(nodes.size ());
    }
    
    /**
     *
     * @throws java.lang.Exception
     * @author Cesar Obach-Renner, cesar AT gurulab.org
     */
    public void stopAll () throws Exception{
        Iterator iter;
        for(iter=nodes.values ().iterator ();iter.hasNext ();){
            ((EPContainer)iter.next ()).stop ();
        }
    }
    
    /**
     * @param envelope
     * @author Cesar Obach-Renner, cesar AT gurulab.org
     * @throws gurulab.merlin.core.CantHandException
     */
    public void sendToAll (Envelope envelope) throws CantHandException{
        Iterator iter;
        for(iter=nodes.values ().iterator ();iter.hasNext ();){
            ((EPContainer)iter.next ()).handEnvelope (envelope);
        }
    }
    
}